/*
 * ml20gui - The MSN Webcam Recorder
 * This source code is under the GNU Public License (GPL).
 * Written by Ramiro Polla
 * Initial release: june 3rd, 2005
 *
 * this program records MSN Webcam Video
 */

#include "ml20gui.h"

/* write */

static int reg_write_createkey( HKEY *hKey )
{
    return RegCreateKeyEx( HKEY_CURRENT_USER,
        "SOFTWARE\\MSN Webcam Recorder",
        0, NULL, REG_OPTION_NON_VOLATILE,
        KEY_WRITE, NULL, hKey, NULL );
}

void reg_write_dword ( char *name, uint32_t  dword )
{
    HKEY hKey;

    if( reg_write_createkey( &hKey ) != ERROR_SUCCESS )
        return;

    RegSetValueEx( hKey, name, 0, REG_DWORD, (uint8_t*) &dword,
                   sizeof(uint32_t) );

    RegCloseKey( hKey );
}

void reg_write_string( char *name, uint8_t  *str, int  len )
{
    HKEY hKey;

    if( reg_write_createkey( &hKey ) != ERROR_SUCCESS )
        return;

    if( str )
        RegSetValueEx( hKey, name, 0, REG_SZ, (uint8_t*) str, len );
    else
        RegDeleteValue( hKey, name );

    RegCloseKey( hKey );
}

/* read */

static int reg_read_openkey( HKEY *hKey )
{
    return RegOpenKeyEx( HKEY_CURRENT_USER,
        "SOFTWARE\\MSN Webcam Recorder",
        0, KEY_QUERY_VALUE, hKey );
}

int  reg_read_string ( char *name, uint8_t  *str, int *len )
{
    HKEY hKey;
    LONG lRet;

    if( reg_read_openkey( &hKey ) != ERROR_SUCCESS )
        return FALSE;

    lRet = RegQueryValueEx( hKey, name, NULL, NULL, (uint8_t*) str,
                            (DWORD*) len );

    RegCloseKey( hKey );

    if( lRet != ERROR_SUCCESS )
        return FALSE;

    return TRUE;
}

int  reg_read_dword  ( char *name, uint32_t *dword )
{
    DWORD size = sizeof(uint32_t);
    HKEY hKey;
    LONG lRet;

    if( reg_read_openkey( &hKey ) != ERROR_SUCCESS )
        return FALSE;

    lRet = RegQueryValueEx( hKey, name, NULL, NULL, (uint8_t*) dword,
                            (DWORD*) &size );

    RegCloseKey( hKey );

    if( lRet != ERROR_SUCCESS )
        return FALSE;

    return TRUE;
}
